package com.soujiayi.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.soujiayi.adapter.ProductParityListAdapter;
import com.soujiayi.model.Mall;
import com.soujiayi.model.Product;
import com.soujiayi.model.ProductAttributes;
import com.soujiayi.model.SkimHistory;
import com.soujiayi.net.ImageLoader;
import com.soujiayi.store.DatabaseManager;
import com.soujiayi.util.Constants;

public class ProductParityActivity extends Activity {
	
	private ListView productParityList;
	private ProductParityListAdapter productParityListAdapter;
	private List<Mall> listData = new ArrayList<Mall>();
	
	private ImageLoader imageLoader;
	private DatabaseManager databaseManager = new DatabaseManager(ProductParityActivity.this);
	
	private Product product;
	private ProductAttributes attributes;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		((MyApplication) this.getApplication()).addActivity(this);
		setContentView(R.layout.product_parity);
		
		Intent intent = this.getIntent();
		product = (Product) intent.getSerializableExtra(Constants.PRODUCT);
		attributes = new ProductAttributes();
		attributes.setAttrlist(product.getAttr());
		
		imageLoader = new ImageLoader(this, false, 170, 170, 2);
		
		DisplayMetrics dm = new DisplayMetrics();  
		dm = this.getApplicationContext().getResources().getDisplayMetrics();
		int width = (int) (170*dm.density);
		int height = (int) (170*dm.density);
		
		// Download product image
		ImageView productImage = (ImageView)findViewById(R.id.product_detail_parity_Img);
		if(product.getCover() != null){
			String imageUrl = product.getCover().replace(".jpg","." + width + "x" + height + ".jpg");
			imageLoader.DisplayImage(imageUrl, productImage);
		}
		TextView productDescription = (TextView)findViewById(R.id.product_detail_parity_description);
		String productName = "";
		if(product.getName() != null){
			productName = product.getName();
			if(productName.length() > 30){
				productName = productName.substring(0,30) + "...";
			}
		}
		productDescription.setText(product.getName());
		
		TextView productPrice = (TextView)findViewById(R.id.product_detail_parity_price);
		String price_unit = getResources().getString(R.string.price_unit);
		productPrice.setText(product.getSaleprice() + price_unit);
		
		TextView productBrand = (TextView)findViewById(R.id.product_detail_parity_brand);
		String brandname = "";
		if(product.getBrandname() != null){
			brandname = product.getBrandname();
		}
		productBrand.append(": " + brandname);
		
		TextView productType = (TextView)findViewById(R.id.product_detail_parity_type);
		//productType.append(": " + "iPod touch");
		productType.setVisibility(View.GONE);
		
		TextView moreParameters = (TextView)findViewById(R.id.product_detail_parity_more_parameters);
		if(attributes.getAttrlist() == null || attributes.getAttrlist().size() <= 0){
			moreParameters.setVisibility(View.GONE);
		}
		moreParameters.setVisibility(View.GONE);
		
		// Initalize user comment
		float reviewscore = product.getReviewscore();
		InitUserComment(reviewscore);
		
		TextView productSameParity = (TextView)findViewById(R.id.product_detail_same_type_parity);
		int mallCount = product.getCompare().getMallnum();
		productSameParity.setText(productSameParity.getText().toString().replace("N", "" + mallCount));
		
		
		// Initialize parity list
		if(product.getCompare() != null && product.getCompare().getMalls() != null){
			listData.addAll(product.getCompare().getMalls());
		}
		productParityList = (ListView)findViewById(R.id.product_parity_ListView);
		productParityListAdapter = new ProductParityListAdapter(this, listData);
		productParityList.setAdapter(productParityListAdapter);
		
		productParityList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(ProductParityActivity.this, ProductDetailActivity.class);
				intent.putExtra(Constants.PRODUCT_IS_PARITY, true);
				intent.putExtra(Constants.PRODUCT_COMPARE_MALL, (Mall)productParityListAdapter.getItem(arg2));
				intent.putExtra(Constants.PRODUCT, product);
				startActivity(intent);
				overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
			}
			
		});
		
		insertSkimHistory();
	}
	
	private void InitUserComment(double point) {
		
		TextView productComment = (TextView)findViewById(R.id.product_detail_parity_comment);
		productComment.append(":");
		
		ImageView start1 = (ImageView)findViewById(R.id.product_detail_parity_start1);
		ImageView start2 = (ImageView)findViewById(R.id.product_detail_parity_start2);
		ImageView start3 = (ImageView)findViewById(R.id.product_detail_parity_start3);
		ImageView start4 = (ImageView)findViewById(R.id.product_detail_parity_start4);
		ImageView start5 = (ImageView)findViewById(R.id.product_detail_parity_start5);
		
		if(point == 0.5){
			start1.setImageResource(R.drawable.star_half);
		}else if(point >= 1){
			start1.setImageResource(R.drawable.star_full);
		}
		
		if(point == 1.5){
			start2.setImageResource(R.drawable.star_half);
		}else if(point >= 2){
			start2.setImageResource(R.drawable.star_full);
		}
		
		if(point == 2.5){
			start3.setImageResource(R.drawable.star_half);
		}else if(point >= 3){
			start3.setImageResource(R.drawable.star_full);
		}
		
		if(point == 3.5){
			start4.setImageResource(R.drawable.star_half);
		}else if(point >= 4){
			start4.setImageResource(R.drawable.star_full);
		}
		
		if(point == 4.5){
			start5.setImageResource(R.drawable.star_half);
		}else if(point == 5){
			start5.setImageResource(R.drawable.star_full);
		}
		
		TextView productPoint = (TextView)findViewById(R.id.product_detail_parity_point);
		productPoint.setText(point + productPoint.getText().toString());
		
	}

	
	public void backToHome(View view){
		Intent intent = new Intent(ProductParityActivity.this, IndexActivity.class);
		startActivity(intent);
		this.finish();
	}
	
	public void back(View view){
		this.finish();
		overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
	}
	
	public void gotoComment(View view){
		Intent intent = new Intent(ProductParityActivity.this, ProductCommentActivity.class);
		startActivity(intent);
		overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
	}
	
	public void checkComments(View view){
		Intent intent = new Intent(ProductParityActivity.this, ProductCommentCheckActivity.class);
		startActivity(intent);
		overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
	}

	public void checkParameters(View view){
		Intent intent = new Intent(ProductParityActivity.this, ProductParametersActivity.class);
		intent.putExtra(Constants.PRODUCT_ATTRIBUTE, attributes);
		startActivity(intent);
		overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
	}
	
	public void insertSkimHistory(){
			SkimHistory skimHistory = new SkimHistory();
			skimHistory.setType(Constants.SKIM_HISTORY_TYPE_PRODUCT);
			skimHistory.setName(product.getName());
			skimHistory.setCover(product.getCover());
			skimHistory.setGoodsid(product.getGoodsid());
			skimHistory.setSaleprice(product.getSaleprice());
			skimHistory.setMallid(product.getMallid());
			skimHistory.setMallname(product.getMallname());
			skimHistory.setMallCount(String.valueOf(product.getCompare().getMallnum()));
			databaseManager.insertSKimHistory(skimHistory);
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		//如果按下的是返回键，并且没有重复
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
			return false;
		}
		return false;
	}
	
	@Override
	protected void onDestroy() {
		imageLoader.close();
		databaseManager.closeDB();
		super.onDestroy();
	}
}
